package com.ruoyi.business.factory.llm.user_type;

import com.ruoyi.business.domain.SysUserLoginDriver;
import com.ruoyi.business.factory.llm.AbstractUserTypeTemplate;
import com.ruoyi.business.mapper.SysUserLoginDriverMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * app司机端
 *
 * @author 99
 * @className ChatGPTTemplate
 * @description TODO
 * @date 2023/4/17 11:48
 */
@RequiredArgsConstructor
@Service("app_driver")
@Slf4j
public class AppDriverTemplate extends AbstractUserTypeTemplate {

    private final SysUserLoginDriverMapper sysUserLoginDriverMapper;

    /**
     * 通过短信注册
     *
     * @param phonenumber 手机号
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean registerSms(String phonenumber) {
        SysUser sysUser = this.registerSmsBefore(phonenumber);

        // 具体用户类型表插入，表示这是一个 app 司机
        SysUserLoginDriver sysUserLoginDriver = new SysUserLoginDriver();
        sysUserLoginDriver.setRelevanceUserId(sysUser.getUserId());
        sysUserLoginDriver.setUserType(UserType.APP_DRIVER.getUserType());
        int result = sysUserLoginDriverMapper.insert(sysUserLoginDriver);

        return result > 0;
    }

    /**
     * 通过手机号查询对应用户
     *
     * @param phonenumber 手机号
     * @return
     */
    @Override
    public SysUser selectUserByPhonenumber(String phonenumber) {
        return sysUserLoginDriverMapper.selectUserByPhone(phonenumber);
    }
}
